package com.will.txj.common.result;

import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import org.springframework.http.HttpStatus;

/**
 * @author: wen-yi;
 * @date: 2021/11/21 15:47;
 * @Description: JsonInclude： 过滤为null
 */
@Data
@ApiModel("统一返回对象")
@JsonInclude(JsonInclude.Include.NON_NULL)
public class R {
    @ApiModelProperty("状态")
    private Boolean status;
    @ApiModelProperty("状态码")
    private Integer code;
    @ApiModelProperty("状态信息")
    private String msg;
    @ApiModelProperty("数据")
    private Object data;

    private R(){}

    private R(IRC result, Object data){
        this.status = result.getCode() == 200;
        this.code = result.getCode();
        this.msg = result.getMessage();
        this.data = data;
    }

    private R(HttpStatus status, Object data){
        this.status = status.value() == 200;
        this.code = status.value();
        this.msg = status.getReasonPhrase();
        this.data = data;
    }

    public static R ok(){
        return new R(RC.SUCCESS,null);
    }
    public static R ok(Object data){
        return new R(RC.SUCCESS,data);
    }

    public static R fail(){
        return new R(RC.EXCEPTION_FAIL,null);
    }
    public static R fail(IRC result){
        return new R(result,null);
    }
    /**
     * @param status HTTP状态枚举
     * @return
     */
    public static R fail(HttpStatus status){
        return new R(status,null);
    }

    /*链式编程*/
    public R setCode(Integer code){
        this.code = code;
        return this;
    }
    public R setMessage(String message){
        this.msg = message;
        return this;
    }
    public R setData(Object data){
        this.data = data;
        return this;
    }
}
